package com.mairuide._frame.utils.sheethandler;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.mairuide.bill.DealFlow;
import com.mairuide.exceltitle_dict.pojo.ExceltitleDict;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-19 18:21
 */
public class POIFactory {

    public static Map<String,Object> deal(InputStream inputStream,Map<String , Object> paramMap) throws Exception{
        long a= System.currentTimeMillis();
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.endsWith(paramMap.get("excelName").toString(),"xls")){
            POIFSFileSystem pfs = new POIFSFileSystem(inputStream); // 解析2003版本
            Workbook wb = new HSSFWorkbook(pfs,true);
            Excel03SheetHandler excel03SheetHandler = new Excel03SheetHandler();
            excel03SheetHandler.setParaMap(paramMap);
            excel03SheetHandler.deal(wb);
            List<DealFlow> data = excel03SheetHandler.getData();
            if (data.size()>0){
                excel03SheetHandler.doSomething();
                excel03SheetHandler.setData(new ArrayList<DealFlow>());
            }
            map=excel03SheetHandler.getResultMap();
        }else{
            //1.根据excel报表获取OPCPackage
            OPCPackage opcPackage = OPCPackage.open(inputStream);
            //2.创建XSSFReader
            XSSFReader reader = new XSSFReader(opcPackage);
            //3.获取SharedStringTable对象
            ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(opcPackage);
            //4.获取styleTable对象
            StylesTable stylesTable = reader.getStylesTable();
            //5.创建Sax的xmlReader对象
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
            //6.注册事件处理器
            SheetHandler sheetHandler = new SheetHandler();
            List<ExceltitleDict> exceltitleDictByTitleHead = sheetHandler.getExceltitleDictByTitleHead();
            sheetHandler.setExceltitleDictList(exceltitleDictByTitleHead);
            sheetHandler.setParaMap(paramMap);
            XSSFSheetXMLHandler xmlHandler = new XSSFSheetXMLHandler(stylesTable,readOnlySharedStringsTable, sheetHandler,false);
            xmlReader.setContentHandler(xmlHandler);
            //7.逐行读取
            XSSFReader.SheetIterator sheetIterator = (XSSFReader.SheetIterator) reader.getSheetsData();
            while (sheetIterator.hasNext()) {
                InputStream stream = sheetIterator.next();
                InputSource is = new InputSource(stream);
                xmlReader.parse(is);
            }
            List<DealFlow> data = sheetHandler.getData();
            if (data.size()>0){
                sheetHandler.doSomething();
            }
            map=sheetHandler.getResultMap();
        }
        System.out.println("一共耗时："+(System.currentTimeMillis()-a)/1000);
        return map;
    }
    
    /**
     * 自定义导入
     * @param inputStream
     * @param paramMap
     * @return
     * @throws Exception
     */
    public static void dealMapping(InputStream inputStream,Map<String , Object> paramMap) throws Exception{
        long a= System.currentTimeMillis();
        if (StringUtils.endsWith(paramMap.get("excelName").toString(),"xls")){
            POIFSFileSystem pfs = new POIFSFileSystem(inputStream); // 解析2003版本
            Workbook wb = new HSSFWorkbook(pfs,true);
            Excel03SheetHandlerMapping excel03SheetHandler = new Excel03SheetHandlerMapping();
            excel03SheetHandler.deal(wb,paramMap);
            excel03SheetHandler.doSomething(paramMap.get("uid").toString());
        }else{
            //1.根据excel报表获取OPCPackage
            OPCPackage opcPackage = OPCPackage.open(inputStream);
            //2.创建XSSFReader
            XSSFReader reader = new XSSFReader(opcPackage);
            //3.获取SharedStringTable对象
            ReadOnlySharedStringsTable readOnlySharedStringsTable = new ReadOnlySharedStringsTable(opcPackage);
            //4.获取styleTable对象
            StylesTable stylesTable = reader.getStylesTable();
            //5.创建Sax的xmlReader对象
            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
            //6.注册事件处理器
            ExcelEventParser eep = new ExcelEventParser();
            eep.setParamMap(paramMap);
            XSSFSheetXMLHandler xmlHandler = new XSSFSheetXMLHandler(stylesTable,readOnlySharedStringsTable, eep,false);
            xmlReader.setContentHandler(xmlHandler);
            //7.逐行读取
            XSSFReader.SheetIterator sheetIterator = (XSSFReader.SheetIterator) reader.getSheetsData();
            while (sheetIterator.hasNext()) {
                InputStream stream = sheetIterator.next();
                InputSource is = new InputSource(stream);
                xmlReader.parse(is);
            }
            eep.doSomething(paramMap.get("uid").toString());
        }
        System.out.println("一共耗时："+(System.currentTimeMillis()-a)/1000);
    }
}
